import QtQuick 2.8
import QtQuick.Controls 2.1
import QtQuick.Layouts 1.3

Control {
    id: root

    property alias model: listView.model
    property alias currentIndex: listView.currentIndex

    width: 250
    padding: 0
    background: Rectangle {
        radius: 2
        color: "#DEDEDE"
    }

    function displayDateTime(date) {
        var now = new Date;
        var d1 = new Date(now.getFullYear(), now.getMonth(), now.getDate());
        var d2 = new Date(date.getFullYear(), date.getMonth(), date.getDate());
        var days = Math.floor((d1 - d2) / (24 * 60 * 60 * 1000));
        var weekList = [qsTr("周日"), qsTr("周一"), qsTr("周二"), qsTr("周三"), qsTr("周四"), qsTr("周五"), qsTr("周六")];

        switch(days) {
        case 0: return date.toLocaleString(Qt.locale(), qsTr("HH:mm"));
        case 1: return qsTr("昨天");
        case 2:
        case 3: return weekList[date.getDay()];
        default: return date.toLocaleString(Qt.locale(), qsTr("yyyy年M月d日"));
        }
    }

    ListView {
        id: listView
        anchors.fill: parent
        ScrollBar.vertical: ScrollBar { }
        snapMode: ListView.SnapToItem
        flickDeceleration: 2000
        cacheBuffer: 200
        clip: true
        model: 100
        delegate: ItemDelegate {
            id: control
            width: root.width
            height: 64
            highlighted: ListView.isCurrentItem
            onClicked: listView.currentIndex = index
            text: index
            contentItem: GridLayout {
                anchors.fill: parent
                anchors.margins: 8
                columnSpacing: 8
                columns: 3
                Image {
                    id: avatar
                    Layout.rowSpan: 2
                    Layout.alignment: Qt.AlignCenter
                    Layout.preferredWidth: 40
                    Layout.preferredHeight: 40
                    source: "qrc:/images/icon_user_back.png" // headImgUrl
                    sourceSize.width: width
                    sourceSize.height: height
                    onStatusChanged: {
                        if(avatar.status == Image.Error) {
                            avatar.source = "qrc:/images/icon_user_back.png";
                        }
                    }
                }
                Text {
                    Layout.preferredWidth: 100
                    Layout.preferredHeight: 20
                    elide: Text.ElideRight
                    font.pixelSize: 12
                    color: "#000000"
//                    text: qsTr("扬州航盛科技有限公司大群")
                     text: displayName
                }
                Text {
                    Layout.preferredWidth: 60
                    Layout.preferredHeight: 20
                    horizontalAlignment: Text.AlignRight
                    font.pixelSize: 12
                    color: "#999999"
//                    text: index%2==0 ? qsTr("今天") : qsTr("2019年1月8日")
                    text: displayDateTime(digestTime)
                }
                Text {
                    Layout.columnSpan: 2
                    Layout.preferredWidth: 140
                    Layout.preferredHeight: 20
                    elide: Text.ElideRight
                    font.pixelSize: 12
                    color: "#999999"
//                    text: qsTr("老师：友情提醒：今天天气变冷，请多穿衣服！")
                     text: digestMsg
                }
            }
            background: Rectangle {
                visible: control.down || control.highlighted || control.visualFocus
                color: "#C2C2C2"
            }
        }
    }
}
